package com.woniuxy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.controller.form.FindGoodsLikeForm;
import com.woniuxy.controller.form.GoodsAddForm;
import com.woniuxy.controller.form.PageAllGoodsForm;
import com.woniuxy.controller.info.FindGoodsLikeInfo;
import com.woniuxy.controller.info.PageAllGoodsInfo;
import com.woniuxy.controller.info.ShowAllgoodsinfo;
import com.woniuxy.dao.AccountDao;
import com.woniuxy.dao.PawnGoodsAttrRelDao;
import com.woniuxy.dao.PawnProductCatDao;
import com.woniuxy.model.*;
import com.woniuxy.dao.PawnGoodsDao;
import com.woniuxy.service.PawnGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.CosUpload;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import com.woniuxy.utils.midClass.PicUrl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnGoodsServiceImpl extends ServiceImpl<PawnGoodsDao, PawnGoods> implements PawnGoodsService {

    @Resource
    private AccountDao accountDao;

    @Resource
    private PawnProductCatDao pawnProductCatDao;

    @Resource
    private PawnGoodsAttrRelDao pawnGoodsAttrRelDao;

    @Resource
    private PawnGoodsDao pawnGoodsDao;

    /**
     *
     * @param pageAllGoodsform
     * @return Result data=Page<ShowAllgoodsinfo>
     */
    @Override
    public Result findGoodspage(PageAllGoodsForm pageAllGoodsform) {
        PageAllGoodsForm pageAllGoodsForm = pawnGoodsDao.selectPage(pageAllGoodsform, null);
        PageAllGoodsInfo pageAllGoodsInfo = NewCopyBean.copyOne(pageAllGoodsform, PageAllGoodsInfo.class);
        pageAllGoodsInfo.setShowAllgoodsinfos(NewCopyBean.copyList(pageAllGoodsForm.getRecords(),ShowAllgoodsinfo.class));

        //accountDao.selectById(eachGoodsInfo.getAssessor()).getAccountUsername())
        for(ShowAllgoodsinfo eachGoodsInfo : pageAllGoodsInfo.getShowAllgoodsinfos()){
            String assessorName = "无";
            String inputUserName = "无";
            String SurveyorName = "无";
            if(null != accountDao.selectById(eachGoodsInfo.getAssessor())){
                assessorName = accountDao.selectById(eachGoodsInfo.getAssessor()).getAccountUsername();
            }
            if(null != accountDao.selectById(eachGoodsInfo.getInputUser())){
                inputUserName = accountDao.selectById(eachGoodsInfo.getInputUser()).getAccountUsername();
            }
            if(null != accountDao.selectById(eachGoodsInfo.getSurveyor())){
                SurveyorName = accountDao.selectById(eachGoodsInfo.getSurveyor()).getAccountUsername();
            }
            eachGoodsInfo.setAssessorName(assessorName);
            eachGoodsInfo.setInputUserName(inputUserName);
            eachGoodsInfo.setSurveyorName(SurveyorName);
            //处理未分类
            String catName = "未分类";
            String subCateName = "未分类";
            if(null!=pawnProductCatDao.selectById(eachGoodsInfo.getCatCode())){
                catName = pawnProductCatDao.selectById(eachGoodsInfo.getCatCode()).getCatName();
            }
            if(null!=pawnProductCatDao.selectById(eachGoodsInfo.getSubCateCode())){
                subCateName = pawnProductCatDao.selectById(eachGoodsInfo.getSubCateCode()).getCatName();
            }
            eachGoodsInfo.setCatName(catName);
            eachGoodsInfo.setSubCateName(subCateName);
            eachGoodsInfo.setAllCatName(catName+"-"+subCateName);
        }

        System.out.println(pageAllGoodsInfo);
        return Result.success(200,"查询成功",pageAllGoodsInfo);
    }

    /**
     * 修改商品 无图片修改
     * @param goodsAddForm
     * @return
     */
    @Override
    public Result updateGoodsNoFile(GoodsAddForm goodsAddForm) {
        PawnGoods pawnGoods = NewCopyBean.copyOne(goodsAddForm, PawnGoods.class);
        Integer goodsId = pawnGoods.getGoodsId();
        pawnGoodsDao.update(pawnGoods,new UpdateWrapper<PawnGoods>()
                .eq("goods_id",goodsId));
        // 属性关联表

        List<PawnAttrConf> pawnGoodsAttrRel = JSON.parseArray(goodsAddForm.getAttr(), PawnAttrConf.class);
        for(PawnAttrConf eachAttr : pawnGoodsAttrRel){
            PawnGoodsAttrRel buffer = new PawnGoodsAttrRel();
            buffer.setGoodsId(goodsId);
            buffer.setAttrCode(eachAttr.getAttrCode());
            buffer.setAttrValue(eachAttr.getOptions());
//            pawnGoodsAttrRelDao.insert(buffer);
            pawnGoodsAttrRelDao.update(buffer,new UpdateWrapper<PawnGoodsAttrRel>()
                    .eq("goods_id",buffer.getGoodsId())
                    .eq("attr_code",buffer.getAttrCode()));
        }
        // 设置picImg 内容为json字符串
        // [{pictype: ,picurl: },]
        return Result.success(200,"修改物品成功",null);
    }

    /**
     * 修改商品，含图片
     * @param files
     * @param goodsAddForm
     * @return
     * @throws IOException
     */
    @Override
    public Result updateGoods(MultipartFile[] files, GoodsAddForm goodsAddForm) throws IOException {
        CosUpload cosUpload = new CosUpload();
        cosUpload.setBucketName("stage3-1259815541");
        List<PicUrl> picUrls = new ArrayList<PicUrl>();
        PawnGoods findGoodsObj = pawnGoodsDao.selectOne(new QueryWrapper<PawnGoods>()
                .eq("goods_id", goodsAddForm.getGoodsId()));
        // 调用COS得接口上传文件

        List<PicUrl> originPicUrls= JSON.parseArray(findGoodsObj.getPicUrl(),PicUrl.class);

        System.out.println(originPicUrls);
        for (MultipartFile file : files){
            if(file.getSize()>9){
                System.out.println(file.getSize());
                String url = "goodsImg/"+file.getOriginalFilename()+String.valueOf(System.currentTimeMillis());
                cosUpload.setKey(url);
            Boolean uploadResult = cosUpload.uploadFile(file.getBytes(),file.getSize());
            System.out.println(uploadResult);
                //修改url
                for(PicUrl eachUrl : originPicUrls){
                    if(eachUrl.getPicType().equals(file.getOriginalFilename())){
                        eachUrl.setPicUrl(url);
                    }
                }
            }
        }
        System.out.println(originPicUrls);
        PawnGoods pawnGoods = NewCopyBean.copyOne(goodsAddForm, PawnGoods.class);
        // 设置图片路径
        String picUrlJson = JSON.toJSONString(originPicUrls);
        pawnGoods.setPicUrl(picUrlJson);
        Integer goodsId = pawnGoods.getGoodsId();
        pawnGoodsDao.update(pawnGoods,new UpdateWrapper<PawnGoods>()
        .eq("goods_id",goodsId));
        // 属性关联表

        List<PawnAttrConf> pawnGoodsAttrRel = JSON.parseArray(goodsAddForm.getAttr(), PawnAttrConf.class);
        for(PawnAttrConf eachAttr : pawnGoodsAttrRel){
            PawnGoodsAttrRel buffer = new PawnGoodsAttrRel();
            buffer.setGoodsId(goodsId);
            buffer.setAttrCode(eachAttr.getAttrCode());
            buffer.setAttrValue(eachAttr.getOptions());
//            pawnGoodsAttrRelDao.insert(buffer);
            pawnGoodsAttrRelDao.update(buffer,new UpdateWrapper<PawnGoodsAttrRel>()
            .eq("goods_id",buffer.getGoodsId())
            .eq("attr_code",buffer.getAttrCode()));
        }
        // 设置picImg 内容为json字符串
        // [{pictype: ,picurl: },]
        return Result.success(200,"修改物品成功",null);
    }

    @Override
    public Result findGoodsLike(FindGoodsLikeForm findGoodsLikeForm) {
        QueryWrapper<PawnGoods> qw = new QueryWrapper<>();
        qw.like("goods_name", findGoodsLikeForm.getStr());
        FindGoodsLikeForm pageFindGoodsLikeForm = pawnGoodsDao.selectPage(findGoodsLikeForm,qw);
        FindGoodsLikeInfo pageFindGoodsInfo = NewCopyBean.copyOne(pageFindGoodsLikeForm,FindGoodsLikeInfo.class);
        pageFindGoodsInfo.setShowAllgoodsinfos(NewCopyBean.copyList(pageFindGoodsLikeForm.getRecords(),ShowAllgoodsinfo.class));

        System.out.println(pageFindGoodsLikeForm.getRecords());
        System.out.println(pageFindGoodsInfo);
        for(ShowAllgoodsinfo eachGoodsInfo : pageFindGoodsInfo.getShowAllgoodsinfos()){
            String assessorName = "无";
            String inputUserName = "无";
            String SurveyorName = "无";
            if(null != accountDao.selectById(eachGoodsInfo.getAssessor())){
                assessorName = accountDao.selectById(eachGoodsInfo.getAssessor()).getAccountUsername();
            }
            if(null != accountDao.selectById(eachGoodsInfo.getInputUser())){
                inputUserName = accountDao.selectById(eachGoodsInfo.getInputUser()).getAccountUsername();
            }
            if(null != accountDao.selectById(eachGoodsInfo.getSurveyor())){
                SurveyorName = accountDao.selectById(eachGoodsInfo.getSurveyor()).getAccountUsername();
            }
            eachGoodsInfo.setAssessorName(assessorName);
            eachGoodsInfo.setInputUserName(inputUserName);
            eachGoodsInfo.setSurveyorName(SurveyorName);
            //处理未分类
            String catName = "未分类";
            String subCateName = "未分类";
            if(null!=pawnProductCatDao.selectById(eachGoodsInfo.getCatCode())){
                catName = pawnProductCatDao.selectById(eachGoodsInfo.getCatCode()).getCatName();
            }
            if(null!=pawnProductCatDao.selectById(eachGoodsInfo.getSubCateCode())){
                subCateName = pawnProductCatDao.selectById(eachGoodsInfo.getSubCateCode()).getCatName();
            }
            eachGoodsInfo.setCatName(catName);
            eachGoodsInfo.setSubCateName(subCateName);
            eachGoodsInfo.setAllCatName(catName+"-"+subCateName);
        }


        return Result.success(200,"findlike成功",pageFindGoodsInfo);
    }

    /**
     * 添加商品
     * @param files
     * @param goodsAddForm
     * @return Result data=null
     * @throws IOException
     */
    @Override
    public Result addGoods(MultipartFile[] files, GoodsAddForm goodsAddForm) throws IOException {
        // inputdate update time
        CosUpload cosUpload = new CosUpload();
        cosUpload.setBucketName("stage3-1259815541");
        List<PicUrl> picUrls = new ArrayList<PicUrl>();
        // 调用COS得接口上传文件
        for (MultipartFile file : files){
            String url = "goodsImg/"+file.getOriginalFilename()+String.valueOf(System.currentTimeMillis());
            cosUpload.setKey(url);
            Boolean uploadResult = cosUpload.uploadFile(file.getBytes(),file.getSize());
            System.out.println(uploadResult);
            PicUrl picUrl = new PicUrl(file.getOriginalFilename(),url);
            picUrls.add(picUrl);
        }

        PawnGoods pawnGoods = NewCopyBean.copyOne(goodsAddForm, PawnGoods.class);
        // 设置图片路径
        String picUrlJson = JSON.toJSONString(picUrls);
        pawnGoods.setPicUrl(picUrlJson);
        pawnGoodsDao.insert(pawnGoods);
        //主键回填
        Integer goodsId = pawnGoods.getGoodsId();
        // 属性关联表

        List<PawnAttrConf> pawnGoodsAttrRel = JSON.parseArray(goodsAddForm.getAttr(), PawnAttrConf.class);
        for(PawnAttrConf eachAttr : pawnGoodsAttrRel){
            PawnGoodsAttrRel buffer = new PawnGoodsAttrRel();
            buffer.setGoodsId(goodsId);
            buffer.setAttrCode(eachAttr.getAttrCode());
            buffer.setAttrValue(eachAttr.getOptions());
            pawnGoodsAttrRelDao.insert(buffer);
        }
        // 设置picImg 内容为json字符串
        // [{pictype: ,picurl: },]
        return Result.success(200,"添加物品成功",null);
    }



}
